///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef __BUOLA_PYTHON_PARSER_H_
#define __BUOLA_PYTHON_PARSER_H_

#include <buola/python.h>
#include <buola/python/types.h>

namespace buola { namespace python {

enum EParseStatus
{
    PARSE_OK,
    PARSE_INCOMPLETE,               //more lines expected
    PARSE_INCOMPLETE_QUOTED,        //more lines expected
    PARSE_INCOMPLETE2,              //more lines expected
    PARSE_INCOMPLETE3,              //more lines expected
    PARSE_INCOMPLETE4,              //more lines expected
    PARSE_INDENT_EXPECTED,          //expected an indented block
    PARSE_INDENT_UNEXPECTED,        //unexpected indent
    PARSE_UNINDENT_UNEXPECTED,      //unexpected unindent
    PARSE_INVALID_SYNTAX,           //other syntax error
    PARSE_INVALID_TOKEN,            //some invalid token (only seen it happening with 08 literals)
    PARSE_SINGLE_QUOTE_EOL,         //EOL while scanning string literal
    PARSE_OVERFLOW,                 //expression too long
    PARSE_TABSPACE,                 //inconsistent use of tabs and spaces
    PARSE_WRONG_UNINDENT,           //unindent does not match any outer indentation level
    PARSE_TOODEEP,                  //too many levels of indentation
    PARSE_LINECONT,                 //unexpected character after line continuation
    PARSE_INVALID_CHARACTER,        //invalid character in identifier
    PARSE_UNKNOWN_ERROR             //other error while parsing
};

enum EParseType
{
    PARSE_FILE,
    PARSE_LINE,
    PARSE_EXPRESSION
};
    
struct CParserResult : public UUncopiable
{
    CParserResult()
        :   mNode(nullptr)
    {}
    
    CParserResult(CParserResult &&pRH)
        :   mStatus(pRH.mStatus)
        ,   mPosition(pRH.mPosition)
        ,   mIndentLevel(pRH.mIndentLevel)
        ,   mNode(pRH.mNode)
    {
        pRH.mNode=nullptr;
    }
    
    ~CParserResult();
    
    EParseStatus mStatus;
    int mPosition;      //position of error, if any
    int mIndentLevel;   //recommended indentation level for next line, in case of
                        //incomplete input
    
    void *mNode; //opaque node structure        
};

CParserResult parse_line(const std::string &pLine,EParseType pType=PARSE_LINE);

YCode compile_node(const CParserResult &pResult);
YCode compile_line(const std::string &pLine,EParseType pType=PARSE_LINE);

/*namespace python*/ } /*namespace buola*/ }

#endif
